package com.cuitrip.business.home.travel.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.cuitrip.business.home.recommend.model.HomeCardType;
import com.cuitrip.business.home.trip.model.Facet;
import com.lab.utils.ArrayUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by sheena on 15/11/6.
 */
public class SelectInfo implements Serializable {

    public static final String FROM_HOME_CARD = "app_card";
    public static final String FROM_SEARCH = "search";

    public static final String DEFAULT_GENDER = "-1";
    public static final String MALE = "1";
    public static final String FEMAIL = "2";
    public static final String KEY_WORD_KEY = "keyWord";
    public static final String CITY_KEY = "cityAbbr";
    public static final String TITLE_KEY = "title";
    public static final String SEARCH_PARAMS_KEY = "search_params";
    public static final String KEY_FROM = "from";
    public static final String LANGUAGE_KEY = "language";
    public static final String KEY_TYPE = "type";

    public static final String KEY = "KEY_SELECT_INFO";
    public List<Facet> facetList = new ArrayList<>();
    public List<FilterInfo> filterInfoList = new ArrayList<>();
    private JSONObject search;
    private int type;
    //关键词搜索
    private String keyWords;
    //城市名称
    private String cityAbbr;
    //标题
    private String title;
    //列表来源
    private String from;
    private FilterExp filterExp = new FilterExp();


    public SelectInfo() {
        search = new JSONObject();
    }

    public List<FilterInfo> getFilterInfoList() {
        return filterInfoList;
    }

    public void setFilterInfoList(List<FilterInfo> filterInfoList) {
        this.filterInfoList = filterInfoList;
    }

    public void copySearchParams(String searchParamsText) {
        try {
            JSONObject searchObject = JSONObject.parseObject(searchParamsText);
            copySearchExtras(searchObject);
        } catch (Exception e) {

        }
    }


    private void copySearchExtras(JSONObject jsonObject) {
        if (null == jsonObject) {
            return;
        }
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            /**
             * 去除keyword和cityAbbr以外，其他参数要作为请求带入。
             */
            String key = entry.getKey();
            if (CITY_KEY.equalsIgnoreCase(key)) {
                this.cityAbbr = jsonObject.getString(SelectInfo.CITY_KEY);
            } else if (KEY_WORD_KEY.equalsIgnoreCase(key)) {
                this.keyWords = jsonObject.getString(SelectInfo.KEY_WORD_KEY);
            } else {
                search.put(key, entry.getValue());
            }
        }
    }

    public String getSearchStringExtra(String key) {
        return search.getString(key);
    }

    public boolean isEmpty() {
        int existParam = 0;
        List<FilterInfo> list = filterExp.getFilterExp();
        if (!ArrayUtil.isNullOrEmpty(list)) {
            for (FilterInfo filterInfo : list) {
                if (!filterInfo.getSelectedValueList().isEmpty()) {
                    existParam++;
                }
            }
        }
        return existParam == 0
                && search.isEmpty();
    }

    public String toFilterJSON() {
        JSONObject jsonObject = new JSONObject();
        List<FilterInfo> list = filterExp.getFilterExp();
        if (!ArrayUtil.isNullOrEmpty(list)) {
            for (FilterInfo filterInfo : list) {
                if (FilterInfo.containsType(filterInfo.getSearchKey())) {
                    FilterInfo.FilterType filterType = FilterInfo.getFilterType(filterInfo.getSearchKey());
                    String value = filterType.getOutPutValue(filterInfo);
                    if (!TextUtils.isEmpty(value)) {
                        jsonObject.put(filterType.key, value);
                    }

                }
            }
        }

        for (Map.Entry<String, Object> entry : search.entrySet()) {
            jsonObject.put(entry.getKey(), entry.getValue());
        }
        return jsonObject.toJSONString();
    }

    public boolean hasSearchStringExtra(String key) {
        return search.containsKey(key);
    }

    public Facet retrieveFacetFrom(String key) {
        for (Facet facet : facetList) {
            if (facet.getKey().equals(key)) {
                return facet;
            }
        }
        return null;
    }

    public JSONObject getSearch() {
        return search;
    }

    public void setSearch(JSONObject search) {
        this.search = search;
    }

    public String getKeyWords() {
        return keyWords;
    }

    public void setKeyWords(String keyWords) {
        this.keyWords = keyWords;
    }

    public String getCityAbbr() {
        String cityAbbrString = this.cityAbbr;
        List<String> valueList = filterExp.getSelectedCityAbbr();
        if (!ArrayUtil.isNullOrEmpty(valueList)) {
            cityAbbrString = TextUtils.join(",", valueList);
        }
        return cityAbbrString;
    }

    public void setCityAbbr(String cityAbbr) {
        this.cityAbbr = cityAbbr;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public FilterExp getFilterExp() {
        return filterExp;
    }

    public void setFilterExp(FilterExp filterExp) {
        this.filterExp = filterExp;
        handleFilterInfoList();
        handleDestination();
    }

    /**
     * 过滤filter info
     */
    private void handleFilterInfoList() {
        ArrayList<FilterInfo> originList = filterExp.getFilterExp();
        ArrayList<FilterInfo> mHandledList = new ArrayList<>();

        for (FilterInfo filterInfo : originList) {
            if ((type == HomeCardType.SUBJECT_SEARCH.value || SelectInfo.FROM_SEARCH.equalsIgnoreCase(from))
                    && FilterInfo.CATEGORY_KEY.equals(filterInfo.getSearchKey())) {
                continue;
            }
            //根据当前的searchKey获取facet
            Facet facet = retrieveFacetFrom(filterInfo.searchKey);
            ArrayList<FilterInfo.ValueInfo> values = filterInfo.getValues();
            if (!ArrayUtil.isNullOrEmpty(values)) {
                List<String> validCateKeyList = null;
                if (facet != null) {
                    //获取facet可用的cate list
                    validCateKeyList = facet.getValidCateItemKeyList();
                }
                for (FilterInfo.ValueInfo valueInfo : values) {
                    //如果该search key 有facet，并且包含当前这个value info的值则不显示
                    if (validCateKeyList != null && !validCateKeyList.contains(valueInfo.getValue())) {
                        valueInfo.setShow(false);
                    }
                    /*if (selectedValueList.contains(valueInfo)) {
                        valueInfo.setSelected(true);
                    }*/
                }
                mHandledList.add(filterInfo);
            }
        }
        filterExp.setFilterExp(mHandledList);
    }

    private void handleDestination() {
        if (!TextUtils.isEmpty(cityAbbr)) {
            ArrayList<Destination> destinationList = filterExp.getDestExp();
            for (Destination destination : destinationList) {
                List<Destination.Value> destinationValueList = destination.getList();
                for (Destination.Value value : destinationValueList) {
                    if (value.getAbbr().equalsIgnoreCase(cityAbbr)) {
                        value.setSelected(true);
                    }
                }
            }

        }
    }

    public void addFacetList(List<Facet> facetList) {
        this.facetList.addAll(facetList);
    }

}
